package com.study.shop.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.study.shop.common.ShopException;
import com.study.shop.entity.dto.TrademarkDTO;
import com.study.shop.entity.po.Trademark;
import com.study.shop.entity.common.MyPage;
import com.study.shop.mapper.TrademarkMapper;
import com.study.shop.service.TrademarkService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class TrademarkServiceImpl implements TrademarkService {
    private final TrademarkMapper trademarkMapper;

    @Override
    public MyPage<Trademark> page(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Trademark> list = trademarkMapper.getAll();
        PageInfo<Trademark> pageInfo = new PageInfo<>(list);
        return new MyPage<>(pageInfo);
    }

    @Override
    public boolean save(TrademarkDTO trademarkDTO) {
        Trademark trademark = BeanUtil.copyProperties(trademarkDTO, Trademark.class);
        int i = trademarkMapper.insertSelective(trademark);
        if (i < 1) {
            throw new ShopException("品牌保存失败");
        }
        return true;
    }

    @Override
    public boolean update(TrademarkDTO trademarkDTO) {
        Trademark trademark = BeanUtil.copyProperties(trademarkDTO, Trademark.class);
        int i = trademarkMapper.updateByPrimaryKeySelective(trademark);
        if (i < 1) {
            throw new ShopException("品牌修改失败");
        }
        return true;
    }

    @Override
    public boolean delete(Integer id) {
        int i = trademarkMapper.deleteByPrimaryKey(Long.parseLong(id + ""));
        if (i < 1) {
            throw new ShopException("删除失败");
        }
        return true;
    }
}
